<?php

namespace App\Admin\Metrics;

use App\Admin\Repositories\MemberTask;
use App\Admin\Repositories\OrgDepartmentOwner;
use App\Admin\Repositories\OrgTeamOwner;
use App\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class NewTasks extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title('新增任务数');
        $this->dropdown([
            '7' => '过去 7 天',
            '15' => '过去 15 天',
            '30' => '过去 30 天',
            '90' => '过去 90 天',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '90':
                list('total' => $total, 'items' => $items) = $this->getData(90);
                // 卡片内容
                $this->withContent($total);
                // 图表数据
                $this->withChart($items);
                break;
            case '30':
                list('total' => $total, 'items' => $items) = $this->getData(30);
                // 卡片内容
                $this->withContent($total);
                // 图表数据
                $this->withChart($items);
                break;
            case '15':
                list('total' => $total, 'items' => $items) = $this->getData(15);
                // 卡片内容
                $this->withContent($total);
                // 图表数据
                $this->withChart($items);
                break;
            case '7':
            default:
                list('total' => $total, 'items' => $items) = $this->getData(7);
                // 卡片内容
                $this->withContent($total);
                // 图表数据
                $this->withChart($items);
        }
    }

    public function getData(int $days) : array
    {
        if (app(AdminController::class)->isAdmin()){
            $data_list = app(MemberTask::class)->getTaskCountByDaysFromNow($days);
        }else{
            $team_id_list = [];
            $owner_department_list = app(OrgDepartmentOwner::class)->getDepartmentListByOwnerUid(Auth::guard('admin')->id());
            if ($owner_department_list){
                $department_id_list = $owner_department_list->pluck('department_id')->toArray();
            }
            $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUidAndDepartmentIds(Auth::guard('admin')->id(), $department_id_list);
            if ($owner_team_list){
                $team_id_list = $owner_team_list->pluck('team_id')->unique()->toArray();
            }
            $data_list = app(MemberTask::class)->getTaskCountByDaysFromNow($days, $team_id_list);
        }
        if ($data_list){
            $data_list = collect($data_list);
            $total = $data_list->pluck('num')->sum();
            $list_map = $data_list->keyBy('days')->toArray();
            while($days){
                $the_day = --$days;
                $day_num = date("Y-m-d",strtotime("-{$the_day} day"));
                $list_map[$day_num] = $list_map[$day_num]['num'] ?? 0;
            }
            $items = collect($list_map)->sortKeys()->values()->toArray();
            return [
                'total' => $total ?? 0,
                'items' => $items ?? []
            ];
        }
        return [
            'total' => 0,
            'items' => []
        ];
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
